package com.lyf.controll;

import cn.hutool.json.JSONObject;
import com.lyf.utils.CrossRegion;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/fileList")
    public class FileListControll extends HttpServlet {
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, IOException {
            //解决跨域
            CrossRegion.setCross(response);
            //1.设置响应头
            response.setContentType("text/html;charset=utf-8");
            //2.读取d盘的Kuguu文件夹
            File file = new File("D:\\粤嵌Java\\第二阶段\\vue-demo\\vue3_mlsd\\src\\assets");

            File[] files = file.listFiles();
            List<String> fileList = new ArrayList<>();
            //3.循环写超链接
            for(File f:files){
                //加判断
                if(f.isFile()){
                    fileList.add(f.getName());
                }
            }
            Map<String,Object> map = new HashMap<>();
            map.put("data",fileList);
            JSONObject jo = new JSONObject();
            jo.putAll(map);
            response.getWriter().println(jo.toString());//响应到浏览器
        }

    }
